<?php

namespace App\Http\Service\Impl;

use App\Http\Service\IGoods;
use App\Http\Service\IIndex;
use App\Http\Service\IOrders;
use app\Request;
use Illuminate\Support\Facades\DB;
use phpDocumentor\Reflection\Types\Array_;
use Ramsey\Uuid\Uuid;

use function foo\func;

class OrdersImpl implements IOrders
{
    function orderslist($request)
    {
        // TODO: Implement orderslist() method.
        $where = " where 1=1 ";
        if ($request->order_num) {
            //$where .= " and d.order_num like '%$request->num%' ";
        }
//        return DB::select("SELECT
//                           o.order_num,o.order_time,
//                           case o.order_state
//                           when 1 then '未付款'
//                           when 2 then '已完成'
//                           end order_state,
//                           o.good_name,
//                           o.good_price,
//                           o.good_score,
//                           o.order_price,
//                           case o.order_pay
//                           when 1 then '微信'
//                           when 2 then '支付宝'
//                           end order_pay,
//                           m.username order_payer,
//                           o.order_paytime
//                           FROM
//                           zhyl_sys_order o " . $where . "
//                           LEFT JOIN zhyl_sys_members m ON o.order_payer = m.id limit ?,?",
//            [$request->rows * ($request->page - 1), $request->rows]);


        return DB::select("SELECT
                            o.tradeno,
                            o.createtime,
                            o.price ddjg,
                            m.phone order_payer,
                        CASE
                            o.paystate 
                            WHEN 1 THEN
                            '未付款' 
                            WHEN 2 THEN
                            '已付款' 
                            END order_state,
                            IFNULL(g.goodname,'金点充值') goodname,
                            g.price spjg,
                            g.score spjdjg,
                        CASE
                            o.payact 
                            WHEN 1 THEN
                            '购买模型' 
                            WHEN 2 THEN
                            '金点充值' 
                            END order_pay,
                        CASE 
                            o.paytype
                            when 1 THEN
                            '支付宝'
                            when 2 then 
                            '微信'
                            END paytype
                        FROM
                            zhyl_sys_payorders o 
                            LEFT JOIN zhyl_sys_users m ON o.userid = m.id 
                            LEFT JOIN zhyl_sys_goodslist g on o.goodid=g.id
                        " . $where . " limit ?,? ",[$request->rows * ($request->page - 1), $request->rows]);


    }

    /**
     * 插入客户订单
     * @param $request
     */
    function insertguestorder($custom)
    {
        // TODO: Implement insertguestorder() method.
        date_default_timezone_set('PRC');
        $curdate = date('Y-m-d H:i:s',time());
        $orderdata = array(
            "orderid"    => Uuid::uuid1(),
            "tradeno"    => $custom['tradeno'],
            "createtime" => date('Y-m-d H:i:s',time()),
            "userid"     => $custom['userid'],
            "goodid"     => $custom['goodid'],
            "payact"     => $custom['payact'],
            "price"      => $custom['price'],//订单价格
            "stime"      => $curdate,//插入下载有效期的开始时间
            "etime"      => date("Y-m-d", strtotime("+1 months", strtotime($curdate)))//插入下载有效期的结束时间
        );
        //插入订单，此处需要加上事物，处理订单插入失败时候，需要回滚的一些情况
        //插入订单之后获取id值
        return DB::table("payorders")->insertGetId($orderdata);
    }


}
